// pages/user_info/index.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: -1,
    user: {
    },
    sexs: [{
      id: 1,
      name: '男',
    }, {
      id: 2,
      name: '女'
    }],
    isCircle: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ user_id: options.id });
    this.getUserOther();
  },

  onPullDownRefresh() {
    this.getUserOther();
    wx.stopPullDownRefresh();
  },
  callMobile() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.user.mobile + ''
    })
  },

  /**
   * 
   * @param {*} sex 
   * @param {*} isName  true name转id，反之 id转name
   */
  convSex(sex, isName) {
    for (let i in this.data.sexs) {
      if (isName && sex == this.data.sexs[i].name) {
        return this.data.sexs[i].id;
      } else if (!isName && this.data.sexs[i].id == sex)
        return this.data.sexs[i].name;
    }
    return 0;
  },

  getUserOther() {
    common.post(url.getUserOtherInfo, { user_id: this.data.user_id }, (data) => {
      data.sex = this.convSex(data.gender, false);
      this.setData({ user: data });
      if (data.circle_id) {
        this.getCircleDetail();
      }
    });
  },

  /**
   * 设置关系
   */
  setUserRelation() {
    wx.showLoading({ mask: true });
    common.post(url.setUserRelation, { user_id: this.data.user_id }, (data) => {
      wx.hideLoading();
      let user = this.data.user;
      user.relation = data;
      this.setData({ user: user });
    });
  },

  onChat({ currentTarget }) {
    var data = currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/chat/index?userid=' + data.userid + '&username=' + data.username
    })
  },

  showImages(e) {
    var data = e.target.dataset;
    wx.previewImage({
      current: data.src, // 当前显示图片的http链接
      urls: [data.src] // 需要预览的图片http链接列表
    });
  },

  showLocation() {
    let user = this.data.user;
    wx.openLocation({
      latitude: user.latitude,
      longitude: user.longitude,
      scale: 18
    })
  },

  getCircleDetail() {
    common.post(url.getCircle, { id: this.data.user.circle_id }, (data) => {
      if (data.audit == 'pass') {
        this.setData({
          isCircle: true
        })
      }

    });
  },


})